/** ###################################################################
**     Filename  : bootloader.C
**     Project   : bootloader
**     Processor : 56F8013VFAE
**     Version   : Driver 01.09
**     Compiler  : Metrowerks DSP C Compiler
**     Date/Time : 8/10/2005, 6:26 PM
**     Abstract  :
**         Main module. 
**         Here is to be placed user's code.
**     Settings  :
**     Contents  :
**         No public methods
**
**     Copyright Freescale Semiconductor, Inc. All Rights Reserved.
** ###################################################################*/
/* MODULE bootloader */

/* Including used modules for compiling procedure */
#include "Cpu.h"
#include "Events.h"
#include "SCI1.h"
/* Include shared modules, which are used for whole project */
#include "PE_Types.h"
#include "PE_Error.h"
#include "PE_Const.h"
#include "IO_Map.h"
/* Include custom modules */
#include "bootloader_lib.h"


#pragma section pram_code begin
/*****************************************************************************
* void main(void)
*
* Main application.
*
* In:  n/a
*
* Out: n/a
*****************************************************************************/
void main(void)
{
  UInt16 state;
  char *temp_ptr;
  
  /*** Processor Expert internal initialization. DON'T REMOVE THIS CODE!!! ***/
  PE_low_level_init();
  /*** End of Processor Expert internal initialization.                    ***/

  /* Write your code here */
   sci_tx("MC56F801x Serial Bootloader v1.5\r\n");

   // Initialize R0,R1,M01, and N for circular buffer (including shadows)
   asm {
        swap shadows                    ;// Switch to shadow registers
        moveu.w #(RX_DATA_SIZE-1),M01   ;// Set buffer size
        moveu.w #rx_data,R0             ;// Set starting address
        moveu.w #SCISR,R1               ;// Use R1 for SCI status register
        moveu.w #0,N                    ;// N is unused
        swap shadows                    ;// Switch back to normal registers
   }

   // Initialize globals
   mem_init(rx_data, NULL, RX_DATA_SIZE);
   data_out = data_in = rx_data;
   state = INITIAL_STATE;
   temp_ptr = rx_data;

   do  {
      // State: Initial State
      if (state == INITIAL_STATE) {
         status = 0;
         sci_tx("Waiting for S-record file ...\r\n");
         sci_tx_char(XON);
         state = WAIT_FOR_S;
      }
      
      if (data_in != temp_ptr) {
         // State: Wait for Header "S"
         if (state == WAIT_FOR_S) {
            temp_ptr = data_out;
            if (get_char(&temp_ptr) == 'S') { state = WAIT_FOR_0; }
            else { get_char(&data_out); }
         }
         // State: Wait for Header "0"
         else if (state == WAIT_FOR_0) {
            if (get_char(&temp_ptr) == '0') { state = WAIT_FOR_EOL; }
            else {
               get_char(&data_out);
               state = WAIT_FOR_S;
            }
         }
         // State: Wait for EOL
         else if (state == WAIT_FOR_EOL) {
            if (get_char(&temp_ptr) == '\r') {
               if (!(status & TX_XOFF)) {
                  sci_tx_char(XOFF);
                  status |= TX_XOFF;
               }
               srec_decode();
               temp_ptr = data_out;
            }
         }
      }
      else {
         if (status & TX_XOFF) {
            sci_tx_char(XON);
            status &= ~TX_XOFF;
         }
      }
      // State: Error
      if (status & DOWNLOAD_ERROR) {
         sci_tx("\r\nError: ");
         sci_tx(int_to_string(status));
         sci_tx("\r\n");
         state = INITIAL_STATE;
      }
   } while (status != DOWNLOAD_OK);

   sci_tx("\r\nDownload Complete\r\n");
   while(1){}
}
#pragma section pram_code end


/* END bootloader */
/*
** ###################################################################
**
**     This file was created by Processor Expert
**     for the Freescale 56800 series of microcontrollers.
**
** ###################################################################
*/
